package org.stvd.repository.admin.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.stvd.entities.admin.UserBase;
import org.stvd.repository.admin.UserBaseDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("UserBaseDao")
public class UserBaseDaoImpl extends BaseDaoImpl<UserBase> implements UserBaseDao {
    
    @Override
    public List<UserBase> listUserBase(String loginCode, String nickName, String depIds, String roleIds, String guid) {
        String hql = "SELECT distinct ub from UserBase ub, UserLogin lc, UserDep ud, UserRole ur"
            + " WHERE ub.userId = lc.userId AND ub.userId = ud.userId AND ub.userId = ur.userId"
            + " AND (lc.loginAccount = ?0 OR '' = ?0)"
            + " AND LOCATE(?2, ud.depId) > 0 "
            + " AND (ud.guid = ?4 OR '' = ?4)"
            + " AND LOCATE(?3, ur.roleId) > 0"
            + " AND (ur.guid = ?4 OR '' = ?4)"
            + " AND ub.nickName LIKE ?1"; 
        return findByHQL(hql, loginCode, "%"+nickName+"%", depIds, roleIds, guid);
    }
    
    @Override
    public List<UserBase> listUserBaseByCompId(String compId) {
        String hql = "FROM UserBase WHERE compId = ?0";
        return findByHQL(hql, compId);
    }
    
    @Override
    public List<UserBase> listUserBaseByTeamId(String teamId) {
        String hql = "FROM UserBase WHERE teamId = ?0";
        return findByHQL(hql, teamId);
    }
    
    @Override
    public List<UserBase> listUserBaseByDepCode(String depCode, String guid) {
        String hql = "select T1 from UserBase T1 where "
            + " exists(select T2.depId from UserDep T2,Department T3 where T2.depId=T3.depId "
            + " and T2.userId=T1.userId and T3.depCode = ?0 and T3.guid = ?1) ";
        return findByHQL(hql, depCode, guid);
    }
    
    @Override
    public List<UserBase> listUserBaseByRoleCode(String roleCode, String guid) {
        String hql = "select T1 from UserBase T1 where "
                + " exists(select T2.roleId from UserRole T2, Roles T3 where T2.roleId=T3.roleId "
                + " and T2.userId=T1.userId and T3.roleCode = ?0 and T3.guid = ?1) ";
        return findByHQL(hql, roleCode, guid);
    }

   
}